<template>
  <div>

    <div>
      <el-button type="primary" @click="dialogVisible = true" icon="el-icon-circle-plus">新增司机</el-button>
    </div>
    <br>

    <template>
      <el-table
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="driverId"
          label="司机编号"
          width="80"
          align="center">
        </el-table-column>
        <el-table-column
          prop="carrier.carrierName"
          label="所属承运商"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          prop="driverName"
          label="姓名"
          width="180"
          align="center">
        </el-table-column>
        <el-table-column
          prop="driverPhone"
          label="联系方式"
          width="180"
          align="center">
        </el-table-column>


        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="dialogVisible1 = true,handleEdit(scope.$index, scope.row)" icon="el-icon-s-tools">查看详情
            </el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-remove"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2,4,6]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <!--添加弹出框-->
    <el-dialog
      title="新增司机"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      @close="handCloseListener">
      <el-form ref="driver" :model="newDriver" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="newDriver.driverName" placeholder="请输入姓名" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="newDriver.driverPhone" placeholder="请输入联系方式" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="承运商">
          <el-select v-model="newDriver.carrierId" placeholder="请选择所属承运商" @change="getByCarrierId($event)">
            <el-option
              v-for="item in carriers"
              :key="item.carrierId"
              :label="item.carrierName"
              :value="item.carrierId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车牌号码">
          <el-select v-model="newDriver.carId" placeholder="请选择所属车辆">
            <el-option
              v-for="item in selCar"
              :key="item.carId"
              :label="item.carPlate"
              :value="item.carId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="dialogVisible = false,onSubmit()">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>

    </el-dialog>
    <!--修改司机信息-->

    <el-dialog
      title="修改车辆信息"
      :visible.sync="dialogVisible1"
      width="30%"
      :before-close="handleClose1">
      <el-form ref="driver" :model="driver" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="driver.driverName" placeholder="请输入姓名" style="width: 220px"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="driver.driverPhone" placeholder="请输入联系方式" style="width: 220px"></el-input>
        </el-form-item>

        <el-form-item label="承运商">
          <el-select v-model="driver.carrierId" placeholder="请选择所属承运商" @change="getByCarrierId2($event)">
            <el-option
              v-for="item in carriers"
              :key="item.carrierId"
              :label="item.carrierName"
              :value="item.carrierId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="车牌号码">
          <el-select v-model="driver.carId" placeholder="请选择所属车辆">
            <el-option
              v-for="item in selCar2"
              :key="item.carId"
              :label="item.carPlate"
              :value="item.carId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="dialogVisible1 = false,onSubmit1()">确 定</el-button>
      <el-button @click="dialogVisible1 = false">取 消</el-button>

    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "driver",
  data() {
    return {
      cars: [],
      carriers: [],
      tableData: [],
      currentPage: 1,
      pageSizes: [2, 4, 6],
      pageSize: 2,
      total: 0,

      multipleSelection: [],
      dialogVisible: false,
      dialogVisible1: false,
      newDriver: {
        driverId: '',
        driverName: '',
        driverPhone: '',
        carrierId: '',
        carId: '',
      },
      driver: {
        driverId: '',
        driverName: '',
        driverPhone: '',
        carrierId: '',
        carId: '',
      },
      selCar: [],
      selCar2:[]
    }
  },

  methods: {
    handCloseListener() {
      this.newDriver = {}
    },


    findDriver(currentPage, pageSize) {
      this.$http({
        method: "GET",
        url: "http://localhost:8090/driver",
        params: {
          currentPage: this.currentPage,
          pageSize: this.pageSize
        }
      }).then(resp => {
        console.log(resp)
        this.tableData = resp.data.data.records;
        this.total = resp.data.data.total;
      })
    },
    onSubmit() {
      this.$http({

        method: "POST",
        url: "http://localhost:8090/driver",
        data: this.newDriver
      }).then(resp => {
        this.newDriver = {
          driverId: '',
          driverName: '',
          driverPhone: '',
          carrierId: '',
          carId: '',
        };
        if (resp.data.code === 100000) {
          this.$message({
            message: '新增' + resp.data.msg,
            type: "success",
          });
        } else {
          this.$message.error('新增' + resp.data.msg + '，请重新输入');
          this.findDriver(this.currentPage, this.pageSize);
        }
      })
    },
    onSubmit1() {
      this.$http({
        method: "PUT",
        url: "http://localhost:8090/driver",
        data: this.driver
      }).then(resp => {
        this.driver = {
          driverId: '',
          driverName: '',
          driverPhone: '',
          carrierId: '',
          carId: '',
        };
        this.findDriver(this.currentPage, this.pageSize);
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {
        });
    },
    handleClose1(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {
        });
    },
    findById(id) {
      this.$http({
        type: "GET",
        url: `http://localhost:8090/driver/${id}`
      }).then(resp => {
        // console.log(resp);
        // console.log(resp.data);
        this.driver = resp.data.data
      })
    },
    //编辑的方法
    handleEdit(index, row) {
      let id = row.driverId;
      this.findById(id);
    },
    //删除的方法
    handleDelete(index, row) {
      let id = row.driverId;
      let a = this.currentPage;
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          method: "DELETE",
          url: `http://localhost:8090/driver/${id}`

        }).then(resp => {
          console.log(resp);
          console.log(resp.data);
          if (a > (this.total / this.pageSize)) {
            this.findDriver(a - 1, this.pageSize);
          } else {
            this.findDriver(a, this.pageSize);
          }

        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    },
    // 页面大小的触发函数
    handleSizeChange(val) {

      //拿到当前的页面大小
      this.pageSize = val;
      //重新加载 数据
      this.findDriver(this.currentPage, this.pageSize);
    },
    //当前页面变化触发的函数
    handleCurrentChange(val) {

      this.currentPage = val;
      // 当前第几页发生了变化  重新加载数据
      this.findDriver(this.currentPage, this.pageSize);
    },
    // findall(currentPage,pageSize) {
    //   this.$http({
    //     method: "GET",
    //     url: "http://localhost:8090/car",
    //     params: {
    //       currentPage: currentPage,
    //       pageSize: pageSize
    //     }
    //   }).then(resp => {
    //     this.tableData = resp.data.data.records
    //     this.total = resp.data.data.total;
    //   })
    //
    // },
    getCar() {
      this.$http({
        method: "GET",
        url: "http://localhost:8090/car/all"
      }).then(resp => {
          // console.log(resp.data);
          this.cars = resp.data.data;
        }
      )
    },
    getCarrier() {
      this.$http({
        method: "GET",
        url: "http://localhost:8090/carrier"
      }).then(resp => {
          // console.log(resp.data);
          this.carriers = resp.data.data;
        }
      )
    },
    //联动下拉框
    getByCarrierId(carrierId) {
      this.newDriver.carId=null;



      this.$http({
        method: "GET",
        url: `http://localhost:8090/car/aa/${carrierId}`
      }).then(resp => {
        // console.log(resp.data)
        this.selCar = resp.data.data;
        console.log(resp.data.data)
      })
    },
    //联动下拉框
    getByCarrierId2(carrierId) {
      this.driver.carId=null;



      this.$http({
        method: "GET",
        url: `http://localhost:8090/car/bb/${carrierId}`
      }).then(resp => {
        // console.log(resp.data)
        this.selCar2 = resp.data.data;
        console.log(resp.data.data)
      })
    }


  },
  created() {
    this.findDriver(1, 2);
    this.getCar();
    this.getCarrier()
  }
}
</script>

<style>
</style>
